<?php
namespace app\index\controller;


use app\common\controller\BaseModel;
use app\common\model\RecodeModel;
use app\common\model\SellerModel;
use app\common\model\SettingModel;
use app\common\model\TicketModel;
use app\common\model\UserModel;
use Hooklife\ThinkphpWechat\Wechat;
use think\Request;


/**
 * Class Index
 * @package app\index\controller
 *
 *      1、index() 测试方法
 *      2、_init_Prize 奖池初始化
 *      3、_drawPrize 付费用户抽奖
 *      4、_drawPrize2 未付费用户抽奖
 *      5、_exchange 奖品提现逻辑
 *
 *
 */

class Index extends BaseModel
{

    // 构造方法
    public function  __construct()
    {
        header('Access-Control-Allow-Origin: *');
        parent::__construct();
    }

    public function index(Request $request)
    {
//        $objPHPExcel = \PHPExcel_IOFactory::load('./user.xlsx');
//        $arrExcel = $objPHPExcel->getSheet(5)->toArray();//获取其中的数据
//
//        $num = count($arrExcel);
//
//        $seller = new SellerModel();
//
//        $arr = array();
//        for($i = 2; $i < $num; $i++) {
//            $data = [
//                'uname' => $arrExcel[$i][2],
//                'password' => '123456'
//            ];
//            array_push($arr, $data);
//        }
//
//        $seller->saveAll($arr);
//        var_dump($arr);

//            $str = "江干区";
//            echo substr($str,0,strlen($str)-3);
//        $this->execlToDataBase($request);

//        $pay = new CPay();
//
//        $pay->sendMoney(10, 'ojruW5IHS9aqO0vM2RkQ5A0KTbrY','Jack现金红包','');

          $this->success('开发者博客', 'https://summer.hk9xh1.cn/v2');



        // execl导出数据库
//        $PHPExcel = new \PHPExcel();
//
//        vendor("PHPExcel.PHPExcel"); //方法一
//        $PHPSheet = $PHPExcel->getActiveSheet();
//
//        $PHPSheet->setTitle("订单数据"); //给当前活动sheet设置名称
//
//        $PHPSheet->setCellValue("A1", "ID");
//        $PHPSheet->setCellValue("B1", "订单号");
//        $PHPSheet->setCellValue("C1", "推送时间");
//        $PHPSheet->setCellValue("D1", "公众号");
//        $PHPSheet->setCellValue("E1", "推文位置");
//
//
//        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
//        header('Content-Disposition: attachment;filename="订单数据.xlsx"');
//        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件

//        $data = $this->https_request('https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=M2IBZ-NXLWQ-66W5W-GZUIR-F4LKZ-ENBUP&get_poi=0', 'get', 'json');
//
//        return $data;


    }


    public function init_Prize() {
        $this->_init_Prize();
    }

    /**
     *  初始化活动奖池 200000 个奖品
     *
     *  todo 1、 去掉红包奖的概率。每有一个付款用户就吧红包奖品插入到奖池中。
     *
     */
    private function _init_Prize() {
        $arr = array();
        // 创建一个25000份奖品的奖池
        // 生成3个被1000整除后等于1的数

        for($a = 0;$a < 3; $a++) {
            array_push($arr, mt_rand(8000, 8999));
        }

        for($b = 0; $b < 100; $b++) {
            array_push($arr, mt_rand(5000, 5999));
        }

        // 随机红包奖
//        for($c = 0; $c < 5000; $c++) {
//            array_push($arr, mt_rand(6000, 6999));
//        }

        for($d = 0; $d < 3770; $d++) {
            array_push($arr, mt_rand(2000, 2999));
        }
        for($e = 0; $e < 5000; $e++) {
            array_push($arr, mt_rand(7000, 7999));
        }
        for($f = 0; $f < 11127; $f++) {
            array_push($arr, mt_rand(3000, 3999));
        }

        shuffle($arr);
        $longText = implode(",", $arr);

        $setting = new SettingModel();

        $setting->where('jack_key', 'prizeArr')->update([
            'jack_value' => $longText
        ]);

    }

    // 抽奖
    private function _drawPrize() {

        $setting = new SettingModel();

        // 获取奖池
        $prizeStr = $setting->where('jack_key', 'prizeArr')->value('jack_value');

        // 奖池字符串转化为数组
        $prizeArr = explode(',', $prizeStr);

        // 打乱数组
        shuffle($prizeArr);

        // 取出第一位
        $res =  array_shift($prizeArr);

        // 将打乱的数组再次转化为字符串存入数据库中
        $longText = implode(",", $prizeArr);
        $setting->where('jack_key', 'prizeArr')->update([
            'jack_value' => $longText
        ]);

        // 输出抽取的数字

        return Floor($res / 1000);

    }


    // 未付费用户抽奖
    private function _drawPrize2() {

        $arr = array();

        for($a = 0;$a < 1; $a++) {
            array_push($arr, 1);
        }
        for($b = 0; $b < 5; $b++) {
            array_push($arr, 2);
        }

        for($c = 0; $c < 1; $c++) {
            array_push($arr, 3);
        }

        for($d = 0; $d < 35; $d++) {
            array_push($arr, 4);
        }
        for($e = 0; $e < 1; $e++) {
            array_push($arr, 5);
        }
        for($e = 0; $e < 20; $e++) {
            array_push($arr, 6);
        }
        for($e = 0; $e < 2; $e++) {
            array_push($arr, 7);
        }
        for($e = 0; $e < 35; $e++) {
            array_push($arr, 8);
        }

        shuffle($arr);

        $res = array_shift($arr);
        return $res;

    }



    // 抽奖接口
    /**
     * @param Request $request
     */
    public function drawPrize(Request $request) {

        $uid = $request->param('uid');
        $type = $request->param('type');

        if (empty($uid)) {
            return $this->json_data(0, '100001', '参数不完整', '', '');
        }

        // 1、 判断用户是否有抽奖次数
        $user = new UserModel();
        $recode = new RecodeModel();

        $count = $user->where('id', $uid)->value('count');

        if ($count < 1) {
            return $this->json_data(0, '700001', '抽奖次数已用尽', '', '');
        }

        //

        $is_ticket = $user->where('id', $uid)->value('is_ticket');

        if ($type != $is_ticket) {
            return $this->json_data(0, '700002', '未知错误', '', '');
        }

        if ($type == 0) {
            $num = $this->_drawPrize2();

            // 执行兑奖逻辑

            $this->_exchange2($uid, $num);

            $user->where('id', $uid)->update([
               'count' => --$count
            ]);

            $jp = ['谢谢惠顾', '100元抵购券', '谢谢惠顾', '20元抵购券', '谢谢惠顾', '50元抵购券', '谢谢惠顾', '10元零件券'];
            $recode->data([
                'uid' => $uid,
                'content' => $jp[$num - 1]
            ])->save();

            // 返回客户端结果
            return $this->json_data(1, '000000', '获奖结果', $num, '');

        }

        if ($type == 1) {
            $num = $this->_drawPrize();

            $this->_exchange($uid, $num);

            // 执行兑奖逻辑
            $user->where('id', $uid)->update([
                'count' => --$count
            ]);

            $jp = ['谢谢惠顾', '100元抵购券', '20元抵购券', '谢谢惠顾', 'A5升级券', '红包', '50元抵购券', 'A5免单'];
            $recode->data([
                'uid' => $uid,
                'content' => $jp[$num - 1]
            ])->save();



            return $this->json_data(1, '000000', '获奖结果', $num, '');

        }

    }

    /**
     *
     *
     * @param $uid
     * @param $num
     *
     *  todo 红包的提现的业务逻辑待实现  no
     *
     */
    private function _exchange($uid, $num) {

        $ticket = new TicketModel();
        $userModel = new UserModel();

        if ($num == 2) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '100元购机券',
                'tprice' => 100,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 3) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '20元购机券',
                'tprice' => 20,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 5) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => 'A5升级券',
                'tprice' => 210,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 6) {
            $pay = new CPay();
            $openId = $userModel->where('id', $uid)->value('openId');
            $money = mt_rand(60, 100);
            $pay->sendMoney($money / 10, $openId,'Jack现金红包','');
        }


        if ($num == 7) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '50元购机券',
                'tprice' => 50,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 8) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => 'A5免单券',
                'tprice' => 50,
                'is_all' => 1,
                'is_pass' => 1,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

    }


    private function _exchange2($uid, $num) {

        if ($num == 1 || $num == 3 || $num == 5 || $num == 7) {
            return ;
        }

        $ticket = new TicketModel();

        if ($num == 2) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '100元购机券',
                'tprice' => 100,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 4) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '20元购机券',
                'tprice' => 20,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 6) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '50元购机券',
                'tprice' => 50,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

        if ($num == 8) {
            $data = [
                'uid' => $uid,
                'oid' => 0,
                'tname' => '10元零件抵用券',
                'tprice' => 10,
                'is_all' => 1,
                'is_pass' => 0,
                't_class' => 2,
                'is_check' => 0,
                'validate' => 1554047980
            ];

            $ticket->data($data)->save();
        }

    }






}
